<?php
class ProductAction extends SystemAction {
	function add() {
		$this->display ();
	}
	function save() {
		$p = D ( 'Product' );
		$ps = D ( 'Productsku' );
		$product = array ();
		$pro = array ();
		$pro ['title'] = $_POST ['title'];
		$pro ['cprice'] = $_POST ['cprice'];
		$pro ['vprice'] = $_POST ['vprice'];
		$pro ['unit'] = $_POST ['unit'];
		$pro ['clsid'] = intval ( $_POST ['clsid'] );
		$proskutmp = array ();
		if ($v = $p->create ( $pro )) {
			foreach ( $_POST ['prosku'] as $k => $sku ) {
				$prosku = array ();
				
				$prosku ['prosku'] = $sku;
				$prosku ['prolower'] = $_POST ['prolower'] [$k];
				$prosku ['proweight'] = $_POST ['proweight'] [$k];
				
				if ($vo [] = $ps->create ( $prosku )) {
					if (! in_array ( $sku, $proskutmp )) {
						array_push ( $proskutmp, $sku );
					} else {
						$this->error ( 'SKU编码重复！' );
					}
				} else {
					$this->error ( $ps->getError () );
				}
			}
			
			if (flase !== $p->add ( $v )) {
				$proid = $p->getLastInsID ();
				foreach ( $vo as $k => $v ) {
					$vo [$k] ['proid'] = $proid;
				}
				
				if ($vo) {
					$ps->addAll ( $vo );
					$this->ajaxReturn ( $vo, '数据添加成功！', 1 );
				} else {
					$this->error ( $ps->getError () );
				}
			} else {
				$this->error ( $ps->getError () );
			}
		} else {
			$this->error ( $p->getError () );
		}
	}
	function edit() {
		$proid = intval ( $_GET ['id'] );
		$proinfo = D ( 'Productsetting' )->getProduct ( $proid );
		$this->assign ( 'proid', $proid );
		$this->assign ( 'proinfo', $proinfo );
		$this->display ();
	}
	
	function editsave() {
		$p = D ( 'Product' );
		$ps = D ( 'Productsku' );
		$product = array ();
		$pro = array ();
		$pro ['id'] = $_POST ['id'];
		$pro ['title'] = $_POST ['title'];
		$pro ['cprice'] = $_POST ['cprice'];
		$pro ['vprice'] = $_POST ['vprice'];
		$pro ['unit'] = $_POST ['unit'];
		$pro ['clsid'] = intval ( $_POST ['clsid'] );
		$proskutmp = array ();
		
		if ($v = $p->create ( $pro )) {
			$p->save ( $v );
			foreach ( $_POST ['prosku'] as $k => $sku ) {
				$prosku = array ();
				$prosku ['prosku'] = $sku;
				$prosku ['proid'] = $_POST ['id'];
				$prosku ['skuid'] = $_POST ['skuid'] [$k];
				$prosku ['prolower'] = $_POST ['prolower'] [$k];
				$prosku ['proweight'] = $_POST ['proweight'] [$k];
				if ($vo = $ps->create ( $prosku )) {
					if (! in_array ( $sku, $proskutmp )) {
						array_push ( $proskutmp, $sku );
						if( $prosku ['skuid'] ) {
							$ps->save ( $vo );
						} else {
							$ps->add( $vo );
						}
					} else {
						$this->error ( 'SKU编码重复！' );
					}
				} else {
					$this->error ( "商品规格{$k}:".$ps->getError () );
				}
			}
			$this->ajaxReturn ( '', '数据更新成功！', 1 );
		} else {
			$this->error ( $p->getError () );
		}
	}
	
	function proskudel(){
		$skuid = intval( $_POST['skuid'] );
		$proid = intval( $_POST['proid'] );
		$ps = D ( 'Productsku' );
		$vo = $ps->where("skuid={$skuid} AND proid={$proid}")->delete();
		if( $vo ) {
			$this->ajaxReturn($vo, '删除成功', 1);
		}else {
			$this->error( $ps->getError() );
		}
	}
	
	function prolist() {
		$count = D ( 'Productsetting' )->getProductCount ();
		
		import ( "ORG.Util.Page" ); // 导入分页类
		$p = new Page ( $count, $this->pagesize );
		$prolist = D ( 'Productsetting' )->getProductList ( $p->firstRow, $p->listRows );
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign ( "prolist", $prolist );
		$this->display ();
	}
	
	function inventory(){
		$skuid = intval($_GET['skuid']);
		$invertory = D('Productsku')->where("skuid={$skuid}")->find();
		$invertorylog = D('Productskulog')->where("skuid={$skuid}")->order("ID DESC")->select();
		$this->assign('skuid', $skuid);
		$this->assign('invertory', $invertory);
		$this->assign('invertorylog', $invertorylog);
		$this->display();
	
	}
	
	function inventoryupdate() {
		$skuid = intval($_POST['skuid']);
		$type  = $_POST['type']; 
		$proinventory = intval( $_POST['proinventory'] ) ;	
		if( $type !== "+" && $type !== "-" ) $this->error( '参数非法' );
		$ps = D('Productsku');
		$psl = D('Productskulog');
		
		$invertory = $ps->where("skuid={$skuid}")->find();
	
		if( $skuid && $proinventory && $invertory ) {
			$ps->startTrans();
			$vo = D('Trade')->updateProinventory ( $invertory['prosku'], $proinventory, $_SESSION['seller_nick'], $type , '[系统]:库存盘点', false  );
			
			if ($vo) {
				$ps->commit();
				$this->ajaxReturn ( $vo, '更新成功', 1 );
			} else {
				$ps->rollback();
				$this->error ( $ps->getError () );
			}
		}else {
			$this->error ( '参数非法' );
		}
	}
	function inventoryupdate2 () {
		$skuid = intval($_POST['skuid']);
		 
		$proinventory = intval( $_POST['proinventory'] ) ;
		 
		$ps = D('Productsku');
		$psl = D('Productskulog');
		
		$invertory = $ps->where("skuid={$skuid}")->find();
		
		if( $skuid && $proinventory && $invertory ) {
			$prosku = $invertory['prosku'];
			$ps->startTrans();
			
			$data['proinventory'] = $proinventory;
			//echo $proinventory;
			$vo = $ps->where ( "prosku='{$prosku}'" )->save ( $data );
				
			if ($vo) {
				$ps->commit();
				$this->ajaxReturn ( $vo, '更新成功', 1 );
			} else {
				$ps->rollback();
				$this->error ( $ps->getError () );
			}
		}else {
			$this->error ( '参数非法' );
		}
	}
	function inventorylist(){
		$proskus = D('Productsetting')->getInventoryList();
		$this->assign( 'proskus', $proskus );
		$this->display();	
	}
	
	function skureport () {
		$skusum = D('Productsku')->query('SELECT `proid` , SUM( proinventory ) AS tp_sum, count( `proinventory` ) AS tp_count
FROM  __TABLE__  GROUP BY proid ');
		$pros = D('Product')->select(); 
		$skucount = D('Productsku')->sum('proinventory');
		foreach ( $skusum as $sku ) {
			$skusumn[ $sku['proid']] = $sku;
			
		}
		$this->assign('pro', $pro);
		$this->assign('skusum',$skusumn);	//总数量 总成本
		$this->assign('pros', $pros);	//总库存
		$this->assign('skucount', $skucount);
		$this->display();
	}

}